package com.xll.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xll.entity.BloodPressure;
import com.xll.entity.Food;
import com.xll.entity.SysFile;
import com.xll.mapper.SysFileMapper;
import com.xll.service.SysFileService;
import com.xll.utils.PageResult;
import com.xll.utils.QueryInfo;
import com.xll.utils.Result;
import com.xll.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SysFileServiceImpl  implements SysFileService {

    @Autowired
     private SysFileMapper sysFileMapper;
    @Override
    public Result delete(Long id) {
        sysFileMapper.delete(id);
        return  Result.success("文件刪除成功");
    }

    @Override
    public Result update(SysFile sysFile) {
        sysFileMapper.update(sysFile);
        return  Result.success("文件名更新成功");
    }

    @Override
    public Result findPage(QueryInfo queryInfo) {
        queryInfo.setQueryString(SecurityUtils.getUsername());
        PageHelper.startPage(queryInfo.getPageNumber(), queryInfo.getPageSize());
        Page<SysFile> page = sysFileMapper.findPage(queryInfo.getQueryString());
        return Result.success("文件查询成功", new PageResult(page.getTotal(), page.getResult()));

    }

    @Override
    public Result upload(List<SysFile> sysFile) {
        List<SysFile>  sysFiles = new ArrayList<>();
        for(SysFile sysFile1:sysFile){
            sysFile1.setCreateUser(SecurityUtils.getUsername());
            sysFile1.setName(sysFile1.getFileName());
            String suffix = sysFile1.getFileName().substring(sysFile1.getFileName().lastIndexOf(".") + 1);
            sysFile1.setFileType(suffix);
            sysFiles.add(sysFile1);
        }
        sysFileMapper.insertList(sysFiles);
        return  Result.success("存入数据库成功");
    }

    @Override
    public Result insert(SysFile sysFile) {
        sysFileMapper.insert(sysFile);
        return  Result.success("文件存入数据库成功");
    }
}
